import base64
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP


# 从文件中读取公钥
with open("public_key.pem", "rb") as fp:
    public_key = fp.read()


# 创建秘钥对象
rsa_key = RSA.import_key(public_key)

# 加密明文
content = "今天是一个好日子".encode("utf-8")  # 先编码

pkcs1 = PKCS1_OAEP.new(rsa_key)  # 创建加密对象
cipher_text = pkcs1.encrypt(content)  # 加密
# print(cipher_text)

# 使用base64进行编码，把二进制内容变成可打印的字符串
base_cipher_text = base64.b64encode(cipher_text)
# print(base_cipher_text)
cipher_text = base_cipher_text.decode("utf-8")
# print(cipher_text)

with open("cipher_text.txt", "w", encoding="utf-8") as fp:
    fp.write(cipher_text)

